/**
 * jims
 */
package com.jims.master.modules.operation.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jims.master.modules.operation.entity.*;
import com.jims.master.modules.operation.service.*;
import com.jims.master.modules.sys.entity.OrgDept;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.service.OrgDeptService;
import com.jims.master.modules.sys.service.SystemService;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.web.BaseController;
import com.jims.master.common.utils.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 手术安排Controller
 * @author DT
 * @version 2017-04-13
 */
@Controller
@RequestMapping(value = "${adminPath}/operation/operationSchedule")
public class OperationScheduleController extends BaseController {
	@Autowired
	private SystemService systemService;
	@Autowired
	private OperationScheduleService operationScheduleService;
	@Autowired
	private OperationApplyService operationApplyService;
	@Autowired
	private OperationNameBeforeService operationNameBeforeService;
	@Autowired
	private OperationNameApplyService operationNameApplyService;
	@Autowired
	private OperationDictService operationDictService;
	@Autowired
	private OperationRoomService operationRoomService;
	@Autowired
	private OrgDeptService orgDeptService;
	@ModelAttribute
	public OperationSchedule get(@RequestParam(required=false) String id) {
		OperationSchedule entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = operationScheduleService.get(id);
		}
		if (entity == null){
			entity = new OperationSchedule();
		}
		return entity;
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(OperationSchedule operationSchedule, HttpServletRequest request, HttpServletResponse response, Model model) {
		OperationApply operationApply=new OperationApply();
		Page<OperationApply> applyPage = operationApplyService.findPage(new Page<OperationApply>(request, response), operationApply);
		Page<OperationSchedule> schedulePage = operationScheduleService.findPage(new Page<OperationSchedule>(request, response), operationSchedule);
		model.addAttribute("schedulePage", schedulePage);
		model.addAttribute("applyPage", applyPage);
		return "modules/operation/operationScheduleIndex";
	}
	/**
	 * 手术室与手术间的联动
	 * Controller
	 * @version 2017-04-06
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "findRoom")
	@ResponseBody
	public StringData findRoom(OperationRoom operationRoom) {
		operationRoom.setStatus("0");
		List<OperationRoom> list=operationRoomService.findList(operationRoom);
		Map<String,String> dataMap=new HashMap<String, String>();
		for(OperationRoom operRoom:list){
			dataMap.put(operRoom.getId(),operRoom.getRoomNo());
		}
		StringData data=new StringData();
		data.setDataMap(dataMap);
		return data;
	}
	/**
	 * 手术安排查询
	 * @param operationSchedule
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"check", ""})
	public String check(OperationSchedule operationSchedule, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<OperationSchedule> schedulePage = operationScheduleService.checkPage(new Page<OperationSchedule>(request, response), operationSchedule);
		model.addAttribute("schedulePage", schedulePage);
		OrgDept orgDept = new OrgDept();
		Page<OrgDept> orgDeptList = orgDeptService.findPage(new Page<OrgDept>(request, response), orgDept);
		model.addAttribute("orgDeptList", orgDeptList);
		return "modules/operation/operationScheduleCheck";
	}
	//手术安排
	@RequiresPermissions("user")
	@RequestMapping(value = "formApply")
	public String form(OperationApply operationApply, HttpServletRequest request, HttpServletResponse response, Model model) {
		if (operationApply != null) {
			if (StringUtils.isNotBlank(operationApply.getId())) {
				operationApply = operationApplyService.get(operationApply);
				OperationNameApply operationNameApply = new OperationNameApply();
				operationNameApply.setApplyId(operationApply.getId());
				List<OperationNameApply> operationNameApplys = operationNameApplyService.findList(operationNameApply);
				operationApply.setOperationNameApplys(operationNameApplys);
				OperationStaffApply operationStaffApply = new OperationStaffApply();
				operationStaffApply.setScheduleId(operationApply.getId());
				OperationStaffApply zdStaffApply=operationApplyService.getStaff(operationStaffApply,"0");
				OperationStaffApply myStaffApply=operationApplyService.getStaff(operationStaffApply,"1");
				OperationStaffApply sxStaffApply=operationApplyService.getStaff(operationStaffApply,"2");
				OperationStaffApply mzStaffApply=operationApplyService.getStaff(operationStaffApply,"3");
				OperationStaffApply yzStaffApply=operationApplyService.getStaff(operationStaffApply,"4");
				operationApply.setZdStaffApply(zdStaffApply);
				operationApply.setMyStaffApply(myStaffApply);
				operationApply.setSxStaffApply(sxStaffApply);
				operationApply.setMzStaffApply(mzStaffApply);
				operationApply.setYzStaffApply(yzStaffApply);
			}
		}
		OrgDept orgDept = new OrgDept();
		Page<OrgDept> orgDeptList = orgDeptService.findPage(new Page<OrgDept>(request, response), orgDept);
		model.addAttribute("orgDeptList", orgDeptList);
		OperationDict operationDict = new OperationDict();
		Page<OperationDict> operationDictList = operationDictService.findPage(new Page<OperationDict>(request, response), operationDict);
		model.addAttribute("operationDictList", operationDictList);
		OperationRoom operationRoom = new OperationRoom();
		Page<OperationRoom> operationRoomList = operationRoomService.findPage(new Page<OperationRoom>(request, response), operationRoom);
		if(StringUtils.isNotBlank(operationApply.getDoctorUser())){
			User user=systemService.getUser(operationApply.getDoctorUser());
			operationApply.setDoctorName(user.getName());
		}
		model.addAttribute("operationState", "0");
		model.addAttribute("operationRoomList", operationRoomList);
		model.addAttribute("operationSchedule", operationApply);
		return "modules/operation/operationScheduleForm";
	}
	//手术安排修改
	@RequiresPermissions("user")
	@RequestMapping(value = "formSchedule")
	public String form(OperationSchedule operationSchedule, HttpServletRequest request, HttpServletResponse response, Model model) {
		if (operationSchedule != null) {
			if (StringUtils.isNotBlank(operationSchedule.getId())) {
				operationSchedule = operationScheduleService.get(operationSchedule);
				OperationNameBefore operationNameBefore = new OperationNameBefore();
				operationNameBefore.setScheduleId(operationSchedule.getId());
				List<OperationNameBefore> operationNameBefores = operationNameBeforeService.findList(operationNameBefore);
				operationSchedule.setOperationNameBefores(operationNameBefores);
				OperationStaffBefore operationStaffBefore = new OperationStaffBefore();
				operationStaffBefore.setScheduleId(operationSchedule.getId());
				OperationStaffBefore zdStaffBefore=operationScheduleService.getStaff(operationStaffBefore,"0");
				OperationStaffBefore myStaffBefore=operationScheduleService.getStaff(operationStaffBefore,"1");
				OperationStaffBefore sxStaffBefore=operationScheduleService.getStaff(operationStaffBefore,"2");
				OperationStaffBefore mzStaffBefore=operationScheduleService.getStaff(operationStaffBefore,"3");
				OperationStaffBefore yzStaffBefore=operationScheduleService.getStaff(operationStaffBefore,"4");
				OperationStaffBefore gyStaffBefore=operationScheduleService.getStaff(operationStaffBefore,"6");
				OperationStaffBefore stStaffBefore=operationScheduleService.getStaff(operationStaffBefore,"5");
				operationSchedule.setZdStaffBefore(zdStaffBefore);
				operationSchedule.setMyStaffBefore(myStaffBefore);
				operationSchedule.setSxStaffBefore(sxStaffBefore);
				operationSchedule.setMzStaffBefore(mzStaffBefore);
				operationSchedule.setYzStaffBefore(yzStaffBefore);
				operationSchedule.setGyStaffBefore(gyStaffBefore);
				operationSchedule.setStStaffBefore(stStaffBefore);
			}
		}
		OrgDept orgDept = new OrgDept();
		Page<OrgDept> orgDeptList = orgDeptService.findPage(new Page<OrgDept>(request, response), orgDept);
		model.addAttribute("orgDeptList", orgDeptList);
		OperationDict operationDict = new OperationDict();
		Page<OperationDict> operationDictList = operationDictService.findPage(new Page<OperationDict>(request, response), operationDict);
		model.addAttribute("operationDictList", operationDictList);
		OperationRoom operationRoom = new OperationRoom();
		Page<OperationRoom> operationRoomList = operationRoomService.findPage(new Page<OperationRoom>(request, response), operationRoom);
		model.addAttribute("operationState", "1");
		if(StringUtils.isNotBlank(operationSchedule.getDoctorUser())){
			User user=systemService.getUser(operationSchedule.getDoctorUser());
			operationSchedule.setDoctorName(user.getName());
		}
        else{
            operationSchedule.setDoctorUser(UserUtils.getUser().getId());
            operationSchedule.setDoctorName(UserUtils.getUser().getName());
            operationSchedule.setOperationDeptId(UserUtils.getUser().getOrgDept().getId());
            operationSchedule.setOperationName(UserUtils.getUser().getOrgDept().getDeptName());
        }
		model.addAttribute("operationRoomList", operationRoomList);
		model.addAttribute("operationSchedule", operationSchedule);
		return "modules/operation/operationScheduleForm";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "findByRoomNo")
	public String findByRoomNo(OperationSchedule operationSchedule, Model model) {
		List<OperationSchedule> operationScheduleList=operationScheduleService.findByRoomNo(operationSchedule);
		model.addAttribute("operationScheduleList", operationScheduleList);
		return "modules/operation/operationRoomApplyForm";
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(OperationSchedule operationSchedule, Model model) {
		StringData data=new StringData();
		int i = operationScheduleService.saveOperationSchedule(operationSchedule);
        if(i>0){
            data.setCode("success");
            data.setData("保存成功");
        }else{
            data.setCode("error");
            data.setData("保存失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(OperationSchedule operationSchedule) {
	    StringData data=new StringData();
		int i=operationScheduleService.deleteAll(operationSchedule);
        if(i>0){
            data.setCode("success");
            data.setData("删除成功");
        }else{
            data.setCode("error");
            data.setData("删除失败");
        }
		return data;
	}

}